package com.example.dllo.fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.dllo.adapter.Rich_News_Adapter;
import com.example.dllo.entity.NewsList;
import com.example.dllo.farm_doctor.R;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 2015/12/2.
 */
public class News_Fragment extends Fragment{
    private RecyclerView recyclerView;
    private Rich_News_Adapter rich_news_adapter;
    private RequestQueue queue;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v= inflater.inflate(R.layout.rich_news, null);
        return v;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView)view.findViewById(R.id.news_recyclerView);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        //绑定适配器
        rich_news_adapter = new Rich_News_Adapter(getActivity());
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 1);
        manager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(rich_news_adapter);
//        rich_news_adapter.setOnMyItemClick();
        initData();

    }


    private void initData() {
        queue = Volley.newRequestQueue(getActivity());

        String url = "http://www.nongyisheng.com/article/list?um=_yingyongbao&fr=android&rn=10&tag=0&sys=5.0.2&pn=0&ver=2.1.0&vc=36";
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        Gson gson = new Gson();
                        NewsList result = gson.fromJson(s,
                                NewsList.class);
                        if (result != null) {
                            List<NewsList.ListEntity> listEntities = result.getList();
                            if (listEntities != null) {
                                rich_news_adapter.addData(listEntities);
                            }

                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();

            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("Accept-Encoding","");
                return map;
            }
        };
        queue.add(request);

    }

//    @Override
//    public void onMyItemClick(AnswerList o) {
//        Intent intent = new Intent();
//        intent.setClass(getActivity(),Answer_Content_Activity.class);
//        intent.putExtra("id",o.getId());
//        startActivity(intent);
//    }

}
